﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Ria.Data;
using System.Windows.Shapes;

namespace ExpenseReport
{
    public partial class ModifyExpenseDetailChildWindow : ChildWindow
    {
        private ExpenseReportDetails _detailItem;

        /// <summary>
        /// Allows the current item of the dataform's expense report details to be set.
        /// </summary>
        public ExpenseReportDetails DetailItem
        { 
            get
            {
                return _detailItem;
            }
 
            set
            {
                _detailItem = value;
                detailForm.CurrentItem = value;
            } 
        }

        public ExpenseData ParentDomainContext { get; set; }
        public ExpenseReport ParentExpenseReport { get; set; }

        public ModifyExpenseDetailChildWindow()
        {
            InitializeComponent();
            detailForm.ItemEditEnded += new EventHandler<DataFormItemEditEndedEventArgs>(detailForm_ItemEditEnded);
            detailForm.AutoGeneratingField += new EventHandler<DataFormAutoGeneratingFieldEventArgs>(detailForm_AutoGeneratingField);
        }

        /// <summary>
        /// Prevents ExpenseReport and ExpenseReport ID from showing in the data form.
        /// </summary>
        /// <param name="sender">DataForm</param>
        /// <param name="e">DataFormAutoGeneratingFieldEventArgs generated by DataForm's AutoGeneratingField event</param>
        void detailForm_AutoGeneratingField(object sender, DataFormAutoGeneratingFieldEventArgs e)
        {
            if (e.PropertyName == "ExpenseReport" || e.PropertyName == "expensereportid")
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        ///After item editing is completed, adds the detail item to the parent expense report and domain context.
        ///This allows new items to be cancelled before they are added as children.
        /// </summary>
        /// <param name="sender">DataForm</param>
        /// <param name="e">DataFormAutoGeneratingFieldEventArgs generated by DataForm's AutoGeneratingField event</param>
        void detailForm_ItemEditEnded(object sender, DataFormItemEditEndedEventArgs e)
        {
            ExpenseReportDetails detail = (ExpenseReportDetails)detailForm.CurrentItem;
            if (e.EditAction == DataFormEditAction.Commit && detail.EntityState == EntityState.New && ParentDomainContext != null)
            {
                ParentExpenseReport.ExpenseReportDetails.Add(detail);
                ParentDomainContext.ExpenseReportDetails.Add(detail);

            }
            //This form was called to add a new ExpenseReportDetail.
            if (this.DataContext.Equals(detail))
            {
                ParentDomainContext = null;
                this.Close();
            }
        }
    }
}

